-- ATUALIZA AS OUTRAS TABELAS AMARRADAS NA INCLUSAO, ALTERACAO E EXCLUSAO DA ETIQUETA DA OF ( TRANFERENCIA E CONSUMO )
CREATE OR REPLACE TRIGGER T_ATS_DETOFCONS_ETIQ
BEFORE INSERT OR UPDATE OR DELETE ON ATS_DETOFCONS_ETIQ FOR EACH ROW
DECLARE
v_oflanc NUMBER;
BEGIN
  IF INSERTING THEN  
      UPDATE ATS_DETOFCONS SET DFC_QTDEMP = DFC_QTDEMP + :NEW.OFE_QTDETQ
      WHERE DFC_COFCON= :NEW.OFE_COFCON; 
    
  ELSIF UPDATING THEN
    
    IF :NEW.OFE_QTDETQ <> :OLD.OFE_QTDETQ THEN
       UPDATE ATS_DETOFCONS SET DFC_QTDEMP = DFC_QTDEMP + (:NEW.OFE_QTDETQ - :NEW.OFE_QTDETQ) 
       WHERE DFC_COFCON= :NEW.OFE_COFCON;
    END IF; 
    
    IF :NEW.OFE_QTDCON <> :OLD.OFE_QTDCON THEN

       UPDATE ATS_DETOFCONS SET DFC_QTDCON = DFC_QTDCON + (:NEW.OFE_QTDCON - :NEW.OFE_QTDCON) 
       WHERE DFC_COFCON= :NEW.OFE_COFCON;

    END IF;
  
  ELSIF DELETING THEN
     UPDATE ATS_DETOFCONS SET DFC_QTDCON = DFC_QTDCON - :OLD.OFE_QTDCON  
     WHERE DFC_COFCON= :NEW.OFE_COFCON;  
     
     UPDATE ATS_DETOFCONS SET DFC_QTDEMP = DFC_QTDEMP - :OLD.OFE_QTDETQ 
       WHERE DFC_COFCON= :NEW.OFE_COFCON;
      
  END IF;           
END; 
/